# -*- coding:utf-8 -*-
# !usr/bin/env python

import pexpect
import sys
import os

sys.path.append(os.path.join(sys.path[0], '../..'))
from csi_config import read_setting

wlan = read_setting('wlan')
PASSWD = read_setting('password')

if __name__ == '__main__':
    def error():
        print("Error - EOF! -- zz")
        sys.exit(-1)

    child = pexpect.spawn('sudo dhclient -r')
    try:
        index = child.expect('password')
        if index == 0:
            child.sendline(PASSWD)
    except pexpect.EOF:
        error()
    while child.isalive():
        pass

    child = pexpect.spawn('sudo iw dev %s disconnect' % wlan)
    try:
        index = child.expect('password')
        if index == 0:
            child.sendline(PASSWD)
    except pexpect.EOF:
        error()
    while child.isalive():
        pass
    print("Disonnect %s OK! -- zz" % wlan)

